package com.test.hongbao;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;


public class HongbaoAccessibilityService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if(HongbaoPreference.getInstance(this.getApplicationContext()).isAutoHongbao()){
            final int eventType = event.getEventType();
            if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED || eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
                AccessibilityNodeInfo nodeInfo = event.getSource();
                if (null != nodeInfo) {
                    AccessibilityNodeInfo nodeInfofu = getRootInActiveWindow();
                    if (nodeInfofu != null) {
                        System.out.println("-----------------------------------------------------------------");
                        for (int i = 0; i < nodeInfofu.getChildCount(); i++) {
                            AccessibilityNodeInfo childinfo = nodeInfofu.getChild(i);
                            if (childinfo != null) {
                                System.out.println(childinfo.getClassName());
                                System.out.println(childinfo.getText());
                            }
                        }
                        System.out.println("-----------------------------------------------------------------");

                        {
                            List<AccessibilityNodeInfo> node1 = nodeInfofu.findAccessibilityNodeInfosByText("领取红包");
                            if (!node1.isEmpty()) {
                                System.out.println("找到红包对象");
                                int size = node1.size();
                                AccessibilityNodeInfo cellNode = node1.get(size - 1);
                                if (cellNode.getParent() != null) {
                                    cellNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                }
                                return;
                            }
                        }
                        {
                            List<AccessibilityNodeInfo> node2 = nodeInfofu.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/b43");
                            if (!node2.isEmpty()) {
                                System.out.println("点开红包");
                                int size = node2.size();
                                AccessibilityNodeInfo cellNode = node2.get(size - 1);
                                cellNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                return;
                            }
                        }
                        {
                            List<AccessibilityNodeInfo> node3 = nodeInfo.findAccessibilityNodeInfosByText("红包详情");
                            List<AccessibilityNodeInfo> node4 = nodeInfo.findAccessibilityNodeInfosByText("手慢了");
                            if (!node3.isEmpty() || !node4.isEmpty()) {
                                performGlobalAction(GLOBAL_ACTION_HOME);
                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    @Override
    public void onInterrupt() {

    }
}
